{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "under-rider",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "defined \u001b[32mtrait\u001b[39m \u001b[36mBuffer\u001b[39m\r\n",
       "defined \u001b[32mclass\u001b[39m \u001b[36mSeqBuffer\u001b[39m"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "trait Buffer {\n",
    "    type T\n",
    "    val element:T\n",
    "}\n",
    "\n",
    "abstract class SeqBuffer extends Buffer{\n",
    "    type U\n",
    "    type T <: Seq[U]//define the upline\n",
    "    def length = element.length\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "ranking-morgan",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "length = 2\n",
      "content = List(7, 8)\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "defined \u001b[32mclass\u001b[39m \u001b[36mIntSeqBuffer\u001b[39m\r\n",
       "defined \u001b[32mfunction\u001b[39m \u001b[36mnewIntSeqBuf\u001b[39m\r\n",
       "\u001b[36mbuf\u001b[39m: \u001b[32mIntSeqBuffer\u001b[39m = ammonite.$sess.cmd1$Helper$$anon$1@7ea98a8e"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "abstract class IntSeqBuffer extends SeqBuffer {\n",
    "    type U = Int\n",
    "}\n",
    "\n",
    "def newIntSeqBuf(elem1:Int,elem2:Int):IntSeqBuffer = new IntSeqBuffer{\n",
    "    type T = List[U]\n",
    "    val element = List(elem1,elem2)\n",
    "}\n",
    "val buf = newIntSeqBuf(7,8)\n",
    "println(\"length = \"+ buf.length)\n",
    "println(\"content = \"+ buf.element)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "utility-interference",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "length = 3\n",
      "content = List(7, 8, 10)\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "defined \u001b[32mclass\u001b[39m \u001b[36mBuffer\u001b[39m\r\n",
       "defined \u001b[32mclass\u001b[39m \u001b[36mSeqBuffer\u001b[39m\r\n",
       "defined \u001b[32mfunction\u001b[39m \u001b[36mnewIntSeqBuf\u001b[39m\r\n",
       "\u001b[36mbuf\u001b[39m: \u001b[32mSeqBuffer\u001b[39m[\u001b[32mInt\u001b[39m, \u001b[32mSeq\u001b[39m[\u001b[32mInt\u001b[39m]] = ammonite.$sess.cmd3$Helper$$anon$1@390c0ce3"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "abstract class Buffer[+T]{\n",
    "    val element:T\n",
    "}\n",
    "\n",
    "abstract class SeqBuffer[U,+T<:Seq[U]] extends Buffer[T] {\n",
    "    def length = element.length\n",
    "}\n",
    "\n",
    "def newIntSeqBuf(e1:Int,e2:Int,e3:Int):SeqBuffer[Int,Seq[Int]] = new SeqBuffer[Int,List[Int]] {\n",
    "    val element = List(e1,e2,e3)\n",
    "}\n",
    "\n",
    "val buf = newIntSeqBuf(7,8,10)\n",
    "println(\"length = \"+buf.length)\n",
    "println(\"content = \"+buf.element)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "cleared-belief",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "defined \u001b[32mtrait\u001b[39m \u001b[36mCloneable\u001b[39m\r\n",
       "defined \u001b[32mtrait\u001b[39m \u001b[36mResetable\u001b[39m"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "//复合类型\n",
    "\n",
    "trait Cloneable extends java.lang.Cloneable {\n",
    "    override def clone():Cloneable = {\n",
    "        super.clone().asInstanceOf[Cloneable]\n",
    "    }\n",
    "}\n",
    "\n",
    "\n",
    "trait Resetable {\n",
    "    def reset :Unit\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "european-valuation",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "defined \u001b[32mfunction\u001b[39m \u001b[36mcloneAndReset\u001b[39m"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def cloneAndReset(obj: Cloneable with Resetable): Cloneable = {\n",
    "  val cloned = obj.clone()\n",
    "  obj.reset\n",
    "  cloned\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "jewish-maine",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Scala",
   "language": "scala",
   "name": "scala"
  },
  "language_info": {
   "codemirror_mode": "text/x-scala",
   "file_extension": ".scala",
   "mimetype": "text/x-scala",
   "name": "scala",
   "nbconvert_exporter": "script",
   "version": "2.11.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
